package com.meeruu.sg.account.core.domain.facade.impl;

import com.meeruu.common.model.page.PageParam;
import com.meeruu.common.model.page.PageResult;
import com.meeruu.common.model.result.BaseRpcResult;
import com.meeruu.common.utils.mapper.OrikaMapperUtils;
import com.meeruu.sg.account.core.api.dto.response.ConfigUserWithdrawDTO;
import com.meeruu.sg.account.core.api.service.ConfigUserWithdrawService;
import com.meeruu.sg.account.core.domain.biz.ConfigUserWithdrawBiz;
import com.meeruu.sg.account.core.domain.model.ConfigUserWithdraw;
import com.meeruu.summerframework.cloud.anno.MRSCProvider;
import ma.glasnost.orika.metadata.Type;
import ma.glasnost.orika.metadata.TypeBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@MRSCProvider(serviceInterface = ConfigUserWithdrawService.class)
@Component
public class ConfigUserWithdrawServiceImpl implements ConfigUserWithdrawService {

    @Autowired
    private ConfigUserWithdrawBiz configUserWithdrawBizService;

    @Override
    public BaseRpcResult<ConfigUserWithdrawDTO> queryConfig(Integer levelId) {
        ConfigUserWithdraw withdraw = configUserWithdrawBizService.queryConfig(levelId);
        if (withdraw == null) {
            return new BaseRpcResult<>(null);
        }
        ConfigUserWithdrawDTO dto = OrikaMapperUtils.map(withdraw, OrikaMapperUtils.getType(ConfigUserWithdraw.class),
                                                         OrikaMapperUtils.getType(ConfigUserWithdrawDTO.class));
        return new BaseRpcResult<>(dto);
    }

    @Override
    public BaseRpcResult<PageResult<ConfigUserWithdrawDTO>> queryPage(PageParam pageParam) {
        PageResult<ConfigUserWithdraw> withdraws = configUserWithdrawBizService.queryPage(pageParam);

        Type<PageResult<ConfigUserWithdraw>> fromType = new TypeBuilder<PageResult<ConfigUserWithdraw>>(){}.build();
        Type<PageResult<ConfigUserWithdrawDTO>> toType = new TypeBuilder<PageResult<ConfigUserWithdrawDTO>>(){}.build();
        PageResult<ConfigUserWithdrawDTO> pageResult = OrikaMapperUtils.map(withdraws, fromType, toType);

        return new BaseRpcResult<>(pageResult);
    }

    @Override
    public BaseRpcResult<Void> addConfig(ConfigUserWithdrawDTO withdrawDTO) {
        ConfigUserWithdraw withdraw = OrikaMapperUtils.map(withdrawDTO, OrikaMapperUtils.getType(ConfigUserWithdrawDTO.class),
                                                           OrikaMapperUtils.getType(ConfigUserWithdraw.class));
        configUserWithdrawBizService.addConfig(withdraw);
        return new BaseRpcResult<>();
    }

    @Override
    public BaseRpcResult<Void> modifyConfig(ConfigUserWithdrawDTO withdrawDTO) {
        ConfigUserWithdraw withdraw = OrikaMapperUtils.map(withdrawDTO, OrikaMapperUtils.getType(ConfigUserWithdrawDTO.class),
                                                           OrikaMapperUtils.getType(ConfigUserWithdraw.class));
        configUserWithdrawBizService.modifyConfig(withdraw);
        return new BaseRpcResult<>();
    }

    @Override
    public BaseRpcResult<Void> removeConfig(Integer id) {
        configUserWithdrawBizService.removeConfig(id);
        return new BaseRpcResult<>();
    }
}
